//
//  AzkarPageController.m
//  Azkar
//
//  Created by 王易 on 2018/8/24.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "AzkarPageController.h"
//Tab page view
#import "TYPagerView.h"
#import "TYPagerController.h"
#import "TYTabPagerBar.h"
#import "TYTabPagerView.h"
#import "TYTabPagerController.h"
#import "AzkarDeatailController.h"

/**
祈福容器页面
 @author yzl
 */
@interface AzkarPageController ()<TYTabPagerBarDataSource,TYTabPagerBarDelegate,TYPagerControllerDataSource,TYPagerControllerDelegate>

@property (nonatomic, weak) TYTabPagerBar *tabBar;
@property (nonatomic, weak) TYPagerController *pagerController;
@property (nonatomic, strong) NSArray *datas;

@end

/**
 Azkary页面
 @author yzl
 */
@implementation AzkarPageController
- (UIView *)naviTitleView
{
    TYTabPagerBar *tabBar = [[TYTabPagerBar alloc]initWithFrame:CGRectMake(( 150) *  0.5,[PhoneUtil statusBarHeight], kScreenWidth-150, 44)];
    tabBar.layout.barStyle = TYPagerBarStyleProgressView;
    //    tabBar.layout.cellWidth = CGRectGetWidth(tabBar.frame)/self.datas.count;
    tabBar.layout.cellSpacing = 25;
    tabBar.layout.cellEdging = 0;
    tabBar.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    tabBar.layout.adjustContentCellsCenter = YES;
    tabBar.layout.normalTextFont = [UIFont systemFontOfSize:18.0f weight:UIFontWeightRegular];
    tabBar.layout.selectedTextFont = [UIFont systemFontOfSize:18.0f weight:UIFontWeightSemibold];
    tabBar.layout.normalTextColor = Color(@"#525252");
    tabBar.layout.selectedTextColor = Color(@"#00732F");
    tabBar.layout.progressColor = Color(@"#00732F");
    tabBar.layout.progressHeight = 3.0f;
    tabBar.layout.progressRadius = 1.5f;
    tabBar.dataSource = self;
    tabBar.delegate = self;
    [tabBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
    
    _tabBar = tabBar;
    
    return tabBar;
    


}
- (void)addPagerController {
    TYPagerController *pagerController = [[TYPagerController alloc]init];
    pagerController.layout.prefetchItemCount = 1;
    //    pagerController.layout.autoMemoryCache = NO;
    // 只有当scroll滚动动画停止时才加载pagerview，用于优化滚动时性能
    pagerController.layout.addVisibleItemOnlyWhenScrollAnimatedEnd = YES;
    pagerController.dataSource = self;
    pagerController.delegate = self;
    
    pagerController.view.frame = CGRectMake(0, [PhoneUtil topAdjustHeightWithNavi:YES], ScreenWidth, ScreenHeight - [PhoneUtil topAdjustHeightWithNavi:YES]);
    
    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    _pagerController = pagerController;
}
- (void)initUI
{
    [self addPagerController];
    [self.view addSubview:[self naviTitleView]];
    _datas = @[TLString(@"greeting_cards_azkar_page_NEW"),TLString(@"greeting_cards_azkar_page_TOP")];
    
    [self reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initUI];
    
    [self initNavigation];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}

- (void)initNavigation{
    [self.navigationController setNavigationBarHidden:YES];
    self.myNavigationBar.hidden = YES;
}
#pragma mark - TYPagerControllerDataSource

- (NSInteger)numberOfControllersInPagerController {
    return self.datas.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {

    AzkarDeatailController *vc = [AzkarDeatailController new];    
    vc.top = index == 0 ? nil : @"1";
    return vc;
}

#pragma mark - TYPagerControllerDelegate

- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
    [FIRAnalytics logEventWithName:toIndex==1?@"du_click_top":@"du_click_new"
                        parameters:nil];
}

-(void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}

- (void)reloadData {
    NSInteger index = 0;
    [_pagerController scrollToControllerAtIndex:index animate:YES];
    [_tabBar reloadData];
    [_pagerController reloadData];
}
#pragma mark - TYTabPagerBarDelegate

- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    NSString *title = _datas[index];
    return [pagerTabBar cellWidthForTitle:title]+10.0f;
}

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    [_pagerController scrollToControllerAtIndex:index animate:YES];
}
#pragma mark - TYTabPagerBarDataSource

- (NSInteger)numberOfItemsInPagerTabBar {
    return _datas.count;
}

- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    cell.titleLabel.text = _datas[index];
    return cell;
}


@end
